package com.zeng.sim.support.protocol;

import com.zeng.sim.util.ApplicationContextUtil;
import com.zeng.sim.util.SimConstant;

import java.util.Collection;
import java.util.Iterator;

/**
 * 描述信息
 *
 * @author zengwanlin
 * @date 2023-12-25T15:35
 */
public abstract class MsgProtocolFactory {

    public static IMsgProtocol getMsgProtocol(String code) {
        return getMsgProtocol(SimConstant.MsgProtocol.getInstance(code));
    }

    public static IMsgProtocol getMsgProtocol(SimConstant.MsgProtocol msgProtocol) {
        Collection<IMsgProtocol> protocols = ApplicationContextUtil.getBeansOfType(IMsgProtocol.class);
        Iterator<IMsgProtocol> iterator = protocols.iterator();
        IMsgProtocol protocol = null;
        while (iterator.hasNext()) {
            protocol = iterator.next();
            if (protocol.support(msgProtocol)) {
                return protocol;
            }
        }
        throw new IllegalArgumentException("未知的报文通信协议：" + msgProtocol.getCode());
    }

}
